package com.pan.alg.niuke.tree;

import java.util.ArrayList;
import java.util.List;

public class TreePreorderTraversal {

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param root TreeNode类
     * @return int整型一维数组
     */
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        List<Integer> vals = new ArrayList<>();

        preOrder(root,vals);

        int[] result = new int[vals.size()];
        for (int i = 0; i < vals.size(); i++) {
            result[i]=vals.get(i);
        }
        return result;
    }

    private void preOrder(TreeNode root, List<Integer> vals) {
        if(root==null){
            return;
        }
        vals.add(root.val);
        preOrder(root.left,vals);
        preOrder(root.right,vals);
    }
}
